package com.simple.util.base.extend;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.apache.commons.lang3.StringUtils;

/**
 * @program: simple_tools
 * @description: 拼音工具类
 * @author: Mr.chen
 * @create: 2020-06-09 10:30
 **/
public class PinyinUtil {

    public static String pinyin(char c) {
        String[] pinyins = PinyinHelper.toHanyuPinyinStringArray(c);
        if (pinyins == null) {
            return null;
        }
        return pinyins[0];
    }

    // 使用PinYin4j.jar将汉字转换为拼音
    public static String chineseToPinyin(String chinese) {
        if (StringUtils.isBlank(chinese)) {
            return chinese;
        }
        StringBuffer sb = new StringBuffer();
        char[] chars = chinese.toCharArray();
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        format.setVCharType(HanyuPinyinVCharType.WITH_V);
        for (char c : chars) {
            if (c > 128) {
                try {
                    String strs[] = PinyinHelper.toHanyuPinyinStringArray(c, format);
                    if ((strs != null) && (strs.length > 0)) {
                        sb.append(strs[0]);
                    } else {
                        sb.append(c);
                    }
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }

    public static String convertNumberToHanzi(int i) {
        if (i == 1) {
            return "一";
        }
        if (i == 2) {
            return "二";
        }
        if (i == 3) {
            return "三";
        }
        if (i == 4) {
            return "四";
        }
        if (i == 5) {
            return "五";
        }
        if (i == 6) {
            return "六";
        }

        if (i == 7) {
            return "七";
        }
        if (i == 8) {
            return "八";
        }
        if (i == 9) {
            return "九";
        }
        if (i == 0) {
            return "零";
        }

        return null;
    }

    public static String convertHanziToNumber(String str) {
        if (str.contains("一")) {
            str = str.replaceAll("一", "1");
        }
        if (str.contains("二")) {
            str = str.replaceAll("二", "2");
        }
        if (str.contains("三")) {
            str = str.replaceAll("三", "3");
        }
        if (str.contains("四")) {
            str = str.replaceAll("四", "4");
        }
        if (str.contains("五")) {
            str = str.replaceAll("五", "5");
        }
        if (str.contains("六")) {
            str = str.replaceAll("六", "6");
        }
        if (str.contains("七")) {
            str = str.replaceAll("七", "7");
        }
        if (str.contains("八")) {
            str = str.replaceAll("八", "8");
        }
        if (str.contains("九")) {
            str = str.replaceAll("九", "9");
        }

        if (str.contains("零")) {
            str = str.replaceAll("零", "0");
        }

        return str;
    }

    public static String convertHanziToOldNumber(String str) {
        if(str.contains("萬")){
            str = str.replaceAll("萬","");
        }
        if(str.contains("仟")){
            str = str.replaceAll("仟","");
        }
        if(str.contains("佰")){
            str = str.replaceAll("佰","");
        }
        if(str.contains("拾")){
            str = str.replaceAll("拾","");
        }
        if (str.contains("壹")) {
            str = str.replaceAll("壹", "1");
        }
        if (str.contains("贰")) {
            str = str.replaceAll("贰", "2");
        }
        if (str.contains("叁")) {
            str = str.replaceAll("叁", "3");
        }
        if (str.contains("肆")) {
            str = str.replaceAll("肆", "4");
        }
        if (str.contains("伍")) {
            str = str.replaceAll("伍", "5");
        }
        if (str.contains("陆")) {
            str = str.replaceAll("陆", "6");
        }
        if (str.contains("柒")) {
            str = str.replaceAll("柒", "7");
        }
        if (str.contains("捌")) {
            str = str.replaceAll("捌", "8");
        }
        if (str.contains("玖")) {
            str = str.replaceAll("玖", "9");
        }
        if (str.contains("零")) {
            str = str.replaceAll("零", "0");
        }
        return str;
    }

    /**
     * 中文转unicode
     *
     * @param str
     * @return
     */
    public static String chinaToUnicode(String str) {
        String result = "";
        for (int i = 0; i < str.length(); i++) {
            int chr1 = str.charAt(i);
            if ((chr1 >= 19968) && (chr1 <= 171941)) {// 汉字范围 \u4e00-\u9fa5 (中文)
                result += "\\u" + Integer.toHexString(chr1);
            } else {
                result += str.charAt(i);
            }
        }
        return result;
    }

    public static void main(String[] args) {
        // 13842
        String s = convertHanziToOldNumber("壹萬叁仟捌佰肆拾贰");
        System.out.println(s);
    }
}
